<template>
  <header style="height: 3%; display: flex; margin-bottom: -10px">
    <h1>任务状态查询</h1>
  </header>
  <el-col style="padding: 20px">
    <el-scrollbar>
      <div
        style="
          width: 100%;
          display: flex;
          margin-top: 10px;
          flex-direction: column;
        "
      >
        <div style="display: flex">
          <el-button
            type="primary"
            style="background-color: #003f82; width: 150px"
            @click="UserTaskid"
            >查询当前用户任务</el-button
          >
          <el-button
            type="primary"
            style="background-color: #595959; width: 100px"
            @click="ShowDialog"
            >取消任务</el-button
          >
        </div>
        <el-table :data="response1" style="width: 100%; margin-top: 10px">
          <el-table-column
            prop="仿真演算id"
            label="仿真演算ID"
          ></el-table-column>
          <el-table-column
            prop="仿真演算耗时"
            label="仿真演算耗时"
          ></el-table-column>
          <el-table-column
            prop="仿真验证id"
            label="仿真验证ID"
          ></el-table-column>
          <el-table-column
            prop="仿真验证耗时"
            label="仿真验证耗时"
          ></el-table-column>
        </el-table>
      </div>
      <div
        style="
          width: 100%;
          display: flex;
          margin-top: 10px;
          flex-direction: column;
        "
      >
        <div style="display: flex">
          <el-button
            type="primary"
            style="background-color: #003f82; width: 150px"
            @click="AllUserTaskid"
            >查询所有任务信息</el-button
          >
          <el-button
            icon="WarnTriangleFilled"
            type="danger"
            style="width: 150px"
            @click="dangerTask"
            v-if="userInfo == 'root'"
            >取消所有用户任务</el-button
          >
        </div>
        <el-table
          max-height="400px"
          :data="transformedData"
          style="width: 100%; margin-top: 10px"
        >
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column
            prop="仿真演算id"
            label="仿真演算ID"
          ></el-table-column>
          <el-table-column
            prop="仿真演算耗时"
            label="仿真演算耗时"
          ></el-table-column>
          <el-table-column
            prop="仿真验证id"
            label="仿真验证ID"
          ></el-table-column>
          <el-table-column
            prop="仿真验证耗时"
            label="仿真验证耗时"
          ></el-table-column>
        </el-table>
      </div>
    </el-scrollbar>
  </el-col>
  <el-dialog v-model="isShowDialog" title="根据任务id取消任务">
    <el-form>
      <el-form-item label="任务id:">
        <el-input
          v-model="task.taskid"
          placeholder="请输入对应任务id"
        ></el-input>
      </el-form-item>
      <el-button
        type="primary"
        style="background-color: #595959"
        @click="isShowDialog = false"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="background-color: #003f82"
        @click="FildUserTaskid"
        >确认</el-button
      >
    </el-form>
  </el-dialog>
  <el-dialog v-model="isRemveDialog" title="请等待">
    <!-- <loading-vue></loading-vue> -->
    <loadingVue v-if="loadingVueLoaded" />
  </el-dialog>
</template>

<script setup>
import { defineAsyncComponent, ref, onMounted, computed } from "vue";

import { ElMessage } from "element-plus";
import useUserInfoStore from "@/stores/userInfo.js";
import {
  getAllUserTaskid,
  getUserTaskid,
  taskRevoke,
  allTaskRevoke,
} from "@/server/task";
const userInfoStore = useUserInfoStore();
const userInfo = userInfoStore.info;

let formatDates = (time) => {
  if (time !== "-" && time) {
    time = Number(time);
    if (time < 1000) {
      return time.toFixed(2) + "ms";
    } else if (time < 60000) {
      return (time / 1000).toFixed(2) + "s";
    } else {
      const totalMinutes = Math.floor(time / 60000);
      const minutes = totalMinutes % 60;
      const seconds = Math.floor((time % 60000) / 1000).toFixed(2);
      const hours = Math.floor(totalMinutes / 60);
      return `${hours}h ${minutes}min ${seconds}s`;
    }
  } else {
    return "-";
  }
};
const response1 = ref([]);
const UserTaskid = async () => {
  try {
    const response = await getUserTaskid({
      user: userInfo,
    });
    response1.value.splice(0, response1.value.length);
    response1.value = [];
    for (const key in response) {
      if (response[key].仿真演算耗时 != 0 && response[key].仿真演算id != "0") {
        response[key].仿真演算耗时 = formatDates(response[key].仿真演算耗时);
        console.log(response[key]);
      } else {
        response[key].仿真演算耗时 = "无任务记录";
        response[key].仿真演算id = "无任务记录";
      }
      if (response[key].仿真验证耗时 != 0 && response[key].仿真验证id != "0") {
        response[key].仿真验证耗时 = formatDates(response[key].仿真验证耗时);
        console.log(response[key]);
      } else {
        response[key].仿真验证耗时 = "无任务记录";
        response[key].仿真验证id = "无任务记录";
      }
      response1.value.push(response[key]);
    }
    // response1.value=response;
    console.log(response1.value);
  } catch (e) {
    ElMessage.error("操作失败");
    console.error("e:", e);
  }
};
const isShowDialog = ref(false);
const task = ref({
  taskid: "",
});
const ShowDialog = () => {
  isShowDialog.value = true;
};
const response3 = ref([]);
const FildUserTaskid = async () => {
  try {
    const result = await getUserTaskid({
      user: userInfo,
    });
    response3.value = [];
    for (const key in result) {
      response3.value.push(result[key]);
    }
    console.log(response3.value[0]);
    if (userInfo != "root") {
      if (response3.value[0]) {
        if (
          task.value.taskid == response3.value[0].仿真演算id ||
          task.value.taskid == response3.value[0].仿真验证id
        ) {
          isShowDialog.value = false;
          isRemveDialog.value = true;
          const response = await taskRevoke(task.value);
          if (response.status == "success") {
            isRemveDialog.value = false;
            ElMessage.success("操作成功，任务已取消");
          }
        } else {
          confirm("并非本人任务，禁止删除");
          ElMessage.error("操作失败");
        }
      }
    } else {
      const response = await taskRevoke(task.value);
      if (response.status == "success") {
        isShowDialog.value = false;
        ElMessage.success("操作成功，任务已取消");
      }
    }
  } catch (e) {
    ElMessage.error("操作失败");
    console.error("e:", e);
  }
};
const isRemveDialog = ref(false);
const dangerTask = async () => {
  isRemveDialog.value = true;
  try {
    const isConfirm = confirm("您确定要取消所有任务吗，注意此操作无法回滚！");
    if (isConfirm) {
      const isConfirm1 = confirm(
        "再次确认：您确定要取消所有任务吗，注意此操作无法回滚！"
      );
      if (isConfirm1) {
        const response = await allTaskRevoke();
        if (response.status == "success") {
          isRemveDialog.value = false;
          ElMessage.success("操作成功，任务已取消");
        }
      } else {
        ElMessage.info("操作取消");
        return;
      }
    } else {
      ElMessage.info("操作取消");
      return;
    }
  } catch (e) {
    isRemveDialog.value = false;
    ElMessage.error("操作失败");
    console.error("e:", e);
  }
};

const transformedData = computed(() => {
  const result = [];
  for (const [key, value] of Object.entries(response2.value)) {
    result.push({
      name: key,
      ...value,
    });
  }
  return result;
});

const response2 = ref([]);
const AllUserTaskid = async () => {
  try {
    const response = await getAllUserTaskid();
    for (const key in response) {
      if (response[key].仿真演算耗时 != 0 && response[key].仿真演算id != "0") {
        response[key].仿真演算耗时 = formatDates(response[key].仿真演算耗时);
        console.log(response[key]);
      } else {
        response[key].仿真演算耗时 = "无任务记录";
        response[key].仿真演算id = "无任务记录";
      }
      if (response[key].仿真验证耗时 != 0 && response[key].仿真验证id != "0") {
        response[key].仿真验证耗时 = formatDates(response[key].仿真验证耗时);
        console.log(response[key]);
      } else {
        response[key].仿真验证耗时 = "无任务记录";
        response[key].仿真验证id = "无任务记录";
      }
      response2.value = response;
    }

    console.log(response1.value);
  } catch (e) {
    ElMessage.error("操作失败");
    console.error("e:", e);
  }
};
const loadingVueLoaded = ref(false);
const loadingVue = defineAsyncComponent(() =>
  import("@/components/loading.vue")
);
onMounted(() => {
  if (loadingVue) {
    loadingVueLoaded.value = true;
  }
});
</script>


